package com.julyup.core.service;

import com.julyup.core.dao.CompanyDao;
import com.julyup.core.entity.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业信息存取服务
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2019/4/22 1:07 PM
 */
@Service
public class CompanyService {
  @Autowired
  CompanyDao companyDao;

  public void save(Company entity) {
    companyDao.save(entity);
  }

  public void save(List<Company> companyList) {
    companyList.forEach(company -> {
      if (companyDao.findByCompanyName(company.getCompanyName()) == null) {
        companyDao.save(company);
      }
    });
  }

  public boolean ifPresentInDB(String companyName) {
    Company company = companyDao.findByCompanyName(companyName.trim());
    if (company != null) {
      return true;
    }
    return false;
  }

  public List<Company> findAll() {
    return companyDao.findAll(Sort.by(Sort.Order.desc("regTime")));
  }
}
